<?php
/**
 * 活动投放的卡券
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 4:28
 */
namespace App;

use Model\ActivityCouponMod;
use Model\StoreMod;

class ActivityCoupon extends Action
{
    private $ActivityCoupon = null;   //活动头的卡券
    private $Store = null; //门店

    public function __construct()
    {
        parent::__construct();
        $this->ActivityCoupon = new ActivityCouponMod();
    }

    /**
     * 获取一条投放的卡券信息
     * @param activity_id 活动id
     * @param coupon_id 卡券id
     */
    public function find()
    {
        $_treasure = $this->ActivityCoupon->findOne(array('activity_id' => $this->_gpc['activity_id'], 'coupon_id' => $this->_gpc['coupon_id']), array('id', 'number', 'user_number', 'goal', 'use_limit_type', 'limit_time'));
        if ($_treasure) {
            $this->_result = array('status' => 'success', 'data' => $_treasure);
        }
        $this->result($this->_result);
    }

    /**
     * 投放卡券
     * @param activity_id 活动id
     * @param coupon_id 卡券id
     */
    public function putin()
    {
        //S门店状态判断，非正常门店状态不能投放卡券
        $this->Store = new StoreMod();
        $_store = $this->Store->findOne(array('id'=>$this->_gpc['store_id'],'status'=>1),array('status'));
        if(!$_store){
            $this->_result = array('status'=>'error','err_status'=>9,'err_msg'=>'门店状态不能投放卡券');
            $this->result($this->_result);
        }
        //E门店状态判断，非正常门店状态不能投放卡券

        //S读取基础设置获取投放卡券是否需要审核
        $_activity_setting = $this->getActivitySetting($this->_gpc['activity_id'],array('coupon_check'));
        $this->_gpc['data']['status'] = $_activity_setting ? $_activity_setting['coupon_check'] : 2;
        //E读取基础设置获取投放卡券是否需要审核

        if ($this->_gpc['treasure_id'] == 0) {
            if ($this->ActivityCoupon->insert($this->_gpc['data'])) {
                $this->_result = array('status' => 'success', 'data' => pdo_insertid());
            }
        } else {
            if ($this->ActivityCoupon->update($this->_gpc['data'], array('id' => $this->_gpc['treasure_id']))) {
                $this->_result = array('status' => 'success');
            }
        }
        $this->result($this->_result);
    }

    /**
     * 获取活动投放的列表(门店端获取投放列表)
     * @activity_id 活动id
     * @store_id 门店id
     * @limit 请求下标 请求数量
     * @type 卡券类型
     */
    public function getList()
    {
        $_list = $this->ActivityCoupon->getList($this->_gpc['store_id'], $this->_gpc['activity_id'], $this->_gpc['type'], $this->_gpc['limit']);
        if (count($_list) > 0) {
            foreach ($_list as $_k => $_v) {
                $_list[$_k]['cost'] = json_decode(htmlspecialchars_decode($_v['cost']), true);
                if ($_v['use_limit_type'] == 2) $_list[$_k]['time'] = date('Y-m-d', $_v['limit_time']);
            }
            $this->_result = array('status' => 'success', 'data' => $_list);
        }
        $this->result($this->_result);
    }


    /**
     * 删除取消投放
     * @put_id 投放id
     */
    public function delete()
    {
        if ($this->ActivityCoupon->delete(array('id' => $this->_gpc['put_id']))) {
            $this->_result = array('status' => 'success');
        }
        $this->result($this->_result);
    }

    /**
     * 获取列表总数
     * @param activity_id 活动id
     * @param status 投放卡券的状态
     */
    public function getTotal()
    {
        $_where = array('activity_id' => $this->_gpc['activity_id'], 'status' => $this->_gpc['status']);
        $this->_result = array('status' => 'success', 'data' => $this->ActivityCoupon->total($_where));
        $this->result($this->_result);
    }

    /**
     * 获取活动的投放卡券(活动管理后台获取列表)
     * @param activity_id 活动id
     * @param status 投放卡券的状态
     * @param limit 请求下标个数 [0,10]
     */
    public function adminGetList()
    {
        $_list = $this->ActivityCoupon->adminGetList($this->_gpc['activity_id'], $this->_gpc['status'], $this->_gpc['limit']);
        if (count($_list) > 0) {
            foreach ($_list as $_k => $_v) {
                $_cost = $this->costToStrcost($_v['type'],$_v['cost']);
                $_list[$_k]['type'] = $_cost['type'];
                $_list[$_k]['cost'] = $_cost['face_value'];
                $_list[$_k]['goal'] = $_v['goal'] == 1 ? '宝箱' : '领券页面';
                $_list[$_k]['inventory'] = $_v['give_number'].'/'.$_v['number'];
                $_list[$_k]['period'] = $this->limitTimeTostring($_v['use_limit_type'],$_v['limit_time']);
            }
            $this->_result = array('status' => 'success', 'data' => $_list);
        }
        $this->result($this->_result);
    }


    /**
     * 修改投放卡券的状态
     * @param put_id 投放的id
     * @param status 要修改的状态
     */
    public function changeStatus()
    {
        if($this->ActivityCoupon->update(array('status'=>$this->_gpc['status']),array('id'=>$this->_gpc['put_id']))){
            $this->_result = array('status'=>'success');
        }
        $this->result($this->_result);
    }

    /**
     * 获取指定门店正在投放中的卡券总数
     * @param store_id 门店id
     */
    public function putCouponTotal()
    {
        $_totals = $this->ActivityCoupon->putCouponTotal($this->_gpc['store_id']);
        $this->_result = array('status'=>'success','data'=>abs($_totals));
        $this->result($this->_result);
    }

}